﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FilStartUps.FILAPI;
using System.Drawing;
using System.IO;
using System.Net;
using System.Drawing.Imaging;

namespace FilStartUps
{
    /// <summary>
    /// Summary description for FilStartUps_Images
    /// </summary>
    public class FilStartUps_Images : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            GetPostImage(context);
        }

        private void GetPostImage(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request.QueryString["id"].Trim());
            var image = FilStartUpsAPI.GetPostImage(id);

            byte[] imgpost; 

         
            context.Response.ContentType = "image/jpeg";
           
                if (image == null)
                {
                    //FilStartUps_Default Image Load
                    //Convert Image to base64string and rebirth to Image../Content/images/diagonal.gif
                    Image fils = Image.FromFile("D:/Filstartups/Project/FilStartUps/FilStartUps/Content/images/banner-5.png");

                    string filsbase64 = ImageToBase64(fils, System.Drawing.Imaging.ImageFormat.Png);
                    imgpost = Convert.FromBase64String(filsbase64);
                
                }
                else
                {
                    imgpost = Convert.FromBase64String(image.base64string);
                }
               
                    //imgpost = image.image;
                using (MemoryStream ms = new MemoryStream(imgpost, 0, imgpost.Length))
                {
                    ms.Write(imgpost, 0, imgpost.Length);
                    context.Response.Buffer = true;
                    context.Response.BinaryWrite(imgpost);
                    ms.Dispose();
                    ms.Close();
                }
               // Image img = Base64ToImage(image.base64string);
                 
                

        }

        //Convert To Image
        public Image Base64ToImage(string base64String)
        {
            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0,
              imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);
            return image;
        }

        //Convert To Base64string
        public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                //convert image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                //Convert byte[] to base64string
                string base64string = Convert.ToBase64String(imageBytes);
                return base64string;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}